/********************************************************************************
 * SubCategoriesAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Oct 20, 2008 3:03:13 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.delegator.BannerCampaignServiceDelegator;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.util.SearchCriteria;

public class SubCategoriesAction extends FrontBaseSupport implements ServletRequestAware {

	/** */
	private static final long serialVersionUID = 1L;

	private Logger logger = Logger.getLogger(SubCategoriesAction.class);

	private HttpServletRequest request;

	private String subCategory;

	private GeneralCategories generalCategories;

	private List<BannerCampaigns> bannerCampaignsList;

	private List<GeneralCategoriesWOS> listGeneralCategories = new ArrayList<GeneralCategoriesWOS>();

	public SubCategoriesAction() {
		super(true, true);
		setCategoryDisplay(true);
	}

	public String execute() throws Exception {
		try{
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("generalCategories.categoryId", Integer.parseInt(this.request.getParameter("cid")));
			searchCriteria.setAttribute("active", Boolean.TRUE);
			request.getSession().setAttribute("categoryInSession", this.request.getParameter("cid"));

			List<Criterion> criterionList = new ArrayList<Criterion>();
			List<GeneralCategoriesWOS> tmpGeneralCategories = (List<GeneralCategoriesWOS>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategoriesWOS.class);

			boolean flag = false;
			for (GeneralCategoriesWOS generalCategories : tmpGeneralCategories) {

				if(flag) {
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("generalCategories.categoryId", generalCategories.getCategoryId());

					List<GeneralCategoriesWOS> tmpList = (List<GeneralCategoriesWOS>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, GeneralCategoriesWOS.class);

					if(tmpList.size() > 0) {
						for (GeneralCategoriesWOS generalCategoriesWOS : tmpList) {
							List list = CommonFunctionDelegator.executeSQLQuery("SELECT count(*) FROM items i, general_categories g where i.categoryid = g.categoryid and g.categoryId >= " + generalCategories.getCategoryId() + " and g.mainparentid = " + generalCategories.getMainParentId());
							Object objects = (Object) list.iterator().next();
							generalCategories.setCount(Integer.parseInt(objects.toString()));
						}
					} else {
						generalCategories.setCount(0);
					}
					generalCategories.setGeneralCategoriesList(tmpList);
					this.listGeneralCategories.add(generalCategories);
				} else {
					this.subCategory = generalCategories.getCategoryName();
					flag = true;
				}
			}
			if(criterionList.size() > 0){
				criterionList.clear();
			}
			if((bannerCampaignsList != null)&& (bannerCampaignsList.size() > 0)){
				bannerCampaignsList.clear();
			}
			generalCategories = CategoryManagerServiceDelegator.get(Integer.parseInt(this.request.getParameter("cid")));
			GeneralCategories mainGeneralCategories = CategoryManagerServiceDelegator.get(generalCategories.getMainParentId());
			criterionList.add(Restrictions.or(Restrictions.eq("generalCategories", generalCategories) ,
					Restrictions.or(Restrictions.eq("generalCategories", generalCategories.getGeneralCategories()),
							Restrictions.eq("generalCategories", mainGeneralCategories))));
			this.bannerCampaignsList = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, BannerCampaigns.class);
		}catch (Exception e) {
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

//	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getSubCategory() {
		return subCategory;
	}

	public void setSubCategory(String subCategory) {
		this.subCategory = subCategory;
	}

	public List<GeneralCategoriesWOS> getListGeneralCategories() {
		return listGeneralCategories;
	}

	public void setListGeneralCategories(
			List<GeneralCategoriesWOS> listGeneralCategories) {
		this.listGeneralCategories = listGeneralCategories;
	}

	public GeneralCategories getGeneralCategories() {
		return generalCategories;
	}

	public void setGeneralCategories(GeneralCategories generalCategories) {
		this.generalCategories = generalCategories;
	}

	public List<BannerCampaigns> getBannerCampaignsList() {
		return bannerCampaignsList;
	}

	public void setBannerCampaignsList(List<BannerCampaigns> bannerCampaignsList) {
		this.bannerCampaignsList = bannerCampaignsList;
	}
}
